<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use app\admin\model\Navigator as NavigatorModel;

class Navigator extends AdminBase{

    // 子菜单
    public $subMenu = [];

	/**
	 * TP初始化方法
	 */
	public function _initialize(){
		parent::_initialize();
        $this->subMenu[] = ['title' => '导航列表', 'url' => url('admin/navigator/index'), 'icon' => '&#xe60a;'];
        $this->subMenu[] = ['title' => '添加顶级导航', 'url' => url('admin/navigator/add'), 'icon' => '&#xe654;'];
        $this->assign(['submenu' => $this->subMenu]);
	}

	/**
     * 导航管理
     * @access public
     * @return \think\Response
     */
    public function index(){
        // 实例化导航模型类
        $Navigator = new NavigatorModel;
        // ajax操作
        if($this->request->isAjax()){
            $action = $this->request->param('action');
            if($action == 'del'){
                $ids = $this->request->param('ids/a');
                if(empty($ids)){
                    return mn_ajax_msg(0,'你还没有选择呢');
                }
                $Navigator->delNav($ids);
                return mn_ajax_msg(1,'删除成功');
            }elseif($action == 'order'){
                $order = $this->request->param('data/a');
                if(empty($order)){
                    return mn_ajax_msg(0,'你还没有选择呢');
                }
                $list = [];
                foreach ($order as $key => $value) {
                    if(isset($value['displayorder'])){
                        $Navigator->where('id', (int)$key)->update(['displayorder' => (int)$value['displayorder']]);
                    }
                }
                return mn_ajax_msg(1,'更新排序成功');
            }else{
                return mn_ajax_msg(0,'未知操作');
            }
        }
        // 模板变量赋值
        $this->assign([
            'list' => $Navigator->getTreeTable(),
        ]);
    	return $this->fetch();
    }

    /**
     * 导航添加
     * @access public
     * @param int $pid 上级导航id
     * @return \think\Response
     */
    public function add($pid = 0){
        $pid = (int)$pid;
        // 实例化导航模型类
        $Navigator = new NavigatorModel;
        // 提交操作
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param('data/a');
            // 字段验证并新增数据
            $result = $Navigator->allowField(true)->validate('Navigator')->save($data);
            // 数据操作结果
            if(false !== $result){
                $this->success('添加成功','index');
            }else{
                $this->error($Navigator->getError());
            }
        }
        // 模板变量赋值
        $this->assign([
            'navigator_list' => $Navigator->getTreeOption($pid),
            'system_category' => $this->getCategorys(),
        ]);
        return $this->fetch();
    }

    /**
     * 导航修改
     * @access public
     * @param int $id 导航id
     * @return \think\Response
     */
    public function edit($id = 0){
        $id = (int)$id;
        // 实例化导航模型类
        $Navigator = NavigatorModel::get($id);
        if(!$Navigator){
            $this->error('需要修改的导航不存在');
        }
        // 提交操作
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param('data/a');
            // 父类不能是自己 或者 子类
            if((int)$data['pid'] == $id){
                $this->error('父类不能是自己 或者 子类');
            }
            // 字段验证并修改数据
            $result = $Navigator->allowField(true)->validate('Navigator')->save($data,['id'=>(int)$id]);
            // 数据操作结果
            if(false !== $result){
                $this->success('修改成功','index');
            }else{
                $this->error($Navigator->getError());
            }
        }

        // 模板变量赋值
        $this->assign([
            'data' => $Navigator,
            'navigator_list' => $Navigator->getTreeOption((int)$Navigator->pid),
            'system_category' => $this->getCategorys(),
        ]);
        return $this->fetch();
    }

    /**
     * 删除
     * @access public
     * @return void
     */
    public function del($id = 0){
        $Navigator = new NavigatorModel;
        $Navigator->delNav((int)$id);
        $this->error('删除成功','index');
    }

    /**
     * 更新缓存
     * @access public
     * @return void
     */
    public function cache(){
        
    }
}